<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聊天框</title>
    <link rel="stylesheet" href="./css/basic.css">
    <style>
        .container {
            width: 100%;
            height: 100vh;
            background-color: antiquewhite;
            padding: 10px;
            box-sizing: border-box;
        }
        .content {
            width: 100%;
            height: 75%;
            background-color: #fff;
            box-sizing: border-box;
        }
        .input {
            height: 20%;
            width: 100%;
            position: relative;
            margin-top: none;
        }
        .input textarea, .input button {
            position: absolute;
            resize: none;
        }
        input textarea {
            width: 100%;
            height: 100%;
            padding: 10px;
            box-sizing: border-box;

        }
        .input button {
            right: 10px;
            bottom: 10px;
            width: 100px;
            height: 50px;
            background-color: rgb(173, 222, 239);
            color: #fff;
            font-size: 20px
        }

    </style>
</head>
<body>
    <div class="container">
        <!-- 聊天框 -->
         <div class="content"></div>
         <!-- 输入区 -->
         <div class="input">
            <textarea name="" id="contentArea"></textarea>
            <button id="btnSend">发送</button>
         </div>
    </div>
    <script>
        const btnDom = document.querySelector("#btnSend")//发送对象按钮
        const textareaDom = document.querySelector("#contentArea")// 输入区对象
        const contentDom = document.querySelector(".content")//聊天对象
        enableBtn()
        textareaDom.addEventListener("input",(event)=>{
            enableBtn()//控制按钮是否能点击
        })
        btnDom.addEventListener("click", () => {
        sendMsg()
      })
      textareaDom.addEventListener("keydown", (event) => {
        if(event.key === "Enter") {
          sendMsg()
        }
      })
      function enableBtn () {
        if(textareaDom.value) {
            // 删除disabled属性
            btnDom.removeAttribute("disabled")
        }else {
            // 加上属性
            btnDom.setAttribute("disabled", true)
        }
      }
       // 发送消息
       function sendMsg () {
       // if(textareaDom.value)
       const content = textareaDom.value // 聊天的信息
    //    contentDom.append()
       const p = document.createElement("p")
         p.innerHTML = `<span  class='message'>${content}</span><span class='datetime'>${new Date().toLocaleString()}</span>`

         contentDom.append(p)
         textareaDom.value = ""
      }
    </script>
</body>
</html>